# coding=utf-8

from application import app, redis
from app.model.mysql.user_gift import UserGift as UserGiftMysql
from app.json.mysql.user_gift import UserGift as UserGiftJson


def get_list(u_id):
    mysql_list = UserGiftMysql.query.filter(UserGiftMysql.u_id == u_id, UserGiftMysql.is_delete == 0).all()
    result_list = []
    for mysql_one in mysql_list:
        gift_json = UserGiftJson(mysql_one).getJson()
        result_list.append(gift_json)
    return result_list


def get_cash(token):
    result = None
    user_id = token.split("#")[1] if "#" in token else 0
    if user_id == 0:
        return result

    user_gift_model = UserGiftMysql.query.filter(UserGiftMysql.u_id == user_id, UserGiftMysql.g_id == 1, UserGiftMysql.is_delete == 0).first()

    if not user_gift_model:
        return result
    result = UserGiftJson(user_gift_model)
    return result